package com.sea.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sea.mapper.ArticleMapper;
import com.sea.pojo.Article;
import com.sea.pojo.PageBean;
import com.sea.service.ArticleService;
import com.sea.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper am;

    @Override
    public void add(Article a) {
        a.setCreateTime(LocalDateTime.now());
        a.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        a.setCreateUser(id);
        am.add(a);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        PageBean<Article> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        List<Article> as = am.list(id, categoryId, state);
        Page<Article> p = (Page<Article>) as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Article finda(Integer id) {
        return am.finda(id);
    }

    @Override
    public void updatea(Article a) {
        a.setUpdateTime(LocalDateTime.now());
        am.updatea(a);
    }

    @Override
    public void dlt(Integer id) {
        am.dlt(id);
    }

}
